from py2neo import Graph, Node, NodeMatcher, RelationshipMatcher
import re


class Drug:
    def __init__(self):
        self.graph = Graph("bolt://localhost:7687", username="neo4j", password='1998')
        self.node_matcher = NodeMatcher(self.graph)
        self.rel_matcher = RelationshipMatcher(self.graph)
        self.answer_type = ""
        self.name = ""

    def drug(self, answer_type, drug_name):
        self.answer_type = answer_type
        self.name = drug_name
        if answer_type == "drug_disease":
            self.drug_disease()
        elif answer_type == "drug_form":
            self.drug_form()
        elif answer_type == "drug_function":
            self.drug_function()
        elif answer_type == "drug_usage":
            self.drug_usage()
        elif answer_type == "drug_component":
            self.drug_component()
        elif answer_type == "drug_effects":
            self.drug_effects()
        elif answer_type == "drug_avoid":
            self.drug_avoid()
        elif answer_type == "drug_matters":
            self.drug_matters()

    def drug_disease(self):
        cql = f"match(n:disease)-[r:disease_drug]->(p:drug) where p.name='{self.name}' " \
            f"return n.name as disease order by n.proportion desc"
        data = self.graph.run(cql).data()
        print(data)

    def drug_form(self):
        cql = f"match(n:form)-[r:form_drug]->(p:drug) where p.name='{self.name}' " \
            f"return n.name as form"
        data = self.graph.run(cql).data()
        print(data)

    def drug_function(self):
        cql = f"match(n:drug) where n.name='{self.name}' return n.function as function"
        data = self.graph.run(cql).data()
        print(data)

    def drug_usage(self):
        cql = f"match(n:drug) where n.name='{self.name}' return n.usage as usage"
        data = self.graph.run(cql).data()
        print(data)

    def drug_component(self):
        cql = f"match(n:drug) where n.name='{self.name}' return n.component as component"
        data = self.graph.run(cql).data()
        print(data)

    def drug_effects(self):
        cql = f"match(n:drug) where n.name='{self.name}' return n.effects as effects"
        data = self.graph.run(cql).data()
        print(data)

    def drug_avoid(self):
        cql = f"match(n:drug) where n.name='{self.name}' return n.avoid as avoid"
        data = self.graph.run(cql).data()
        print(data)

    def drug_matters(self):
        cql = f"match(n:drug) where n.name='{self.name}' return n.matters as matters"
        data = self.graph.run(cql).data()
        print(data)


if __name__ == '__main__':
    handler = Drug()
    handler.drug("", "板蓝根颗粒")
    handler.drug_disease()
    handler.drug_form()
    handler.drug_function()
    handler.drug_usage()
    handler.drug_component()
    handler.drug_effects()
    handler.drug_avoid()
    handler.drug_matters()
